package www.com.library.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import www.com.library.view.RecyclerClickListener;

/**
 * popwindow基类
 * @author reeta
 *
 */
public class BasePopWindow {
	public Activity activity;
	public PopupWindow popWindow = null;// pop对象
	protected TextView mTitleView;
	protected RecyclerView popListView;// pop上的listview
	protected View view = null;// 绑定显示视图的view
	protected RecyclerClickListener callback;// 点击事件的回调函数
	protected RecyclerView.Adapter modeListAdapter;
	protected static BasePopWindow mInstance = null;
	protected DialogDismissedListener mDimissedListener = null;// 弹层消失后响应事件
	protected View dialogView;

	public static BasePopWindow  instance(){
		return mInstance;
	}
	
	public void createPopWindow(View dialogView){
		
	}
	
	/**
	 * 初始化pop界面元素和数据
	 * 
	 * @author reeta
	 */
	public void initPopWindow(final Activity activity, View editText,int layoutID,RecyclerClickListener callback) {
		this.activity = activity;
		this.view = editText;
		this.callback = callback;
		final LayoutInflater inflater = LayoutInflater.from(activity);
		dialogView = inflater.inflate(layoutID, null);
		
		createPopWindow(dialogView);

		popWindow = new PopupWindow(dialogView,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		//by reeta 此处必须设置，否则点击事件无效，选择不了
		popWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		popWindow.setOutsideTouchable(true);

		// 设置pop出来后，软键盘的属性，避免pop挡住软键盘，以及pop获取焦点后软键盘会自动隐藏问题
		popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

		// 使用该属性时，在滑动pop的时候不会自动弹出软键盘
		popWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
		
		//为了设置返回按钮关闭弹层
		popWindow.setFocusable(true);
//		backgroundAlpha(activity,0.7f);
		popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				backgroundAlpha(activity,1f);
				if(null != mDimissedListener){
					mDimissedListener.onDismissed();
				}
			}
		});
		dialogView.setOnKeyListener(new OnKeyListener() {
		    @Override
		    public boolean onKey(View v, int keyCode, KeyEvent event) {
		        if (keyCode == KeyEvent.KEYCODE_BACK) {
		        	hidden();

					popWindow.setFocusable(false);
					popWindow.update();
		            return true;
		        }
		        return false;
		    }
		});

	}

	/**
	 * 返回弹层适配器
	 * @return
	 */
	public RecyclerView.Adapter getAdapter(){
		if(null != modeListAdapter){
			return modeListAdapter;
		}
		
		return null;
	}

	/**
	 * 设置添加屏幕的背景透明度
	 * @param bgAlpha
	 */
	public void backgroundAlpha(Activity context, float bgAlpha)
	{
		WindowManager.LayoutParams lp = context.getWindow().getAttributes();
		lp.alpha = bgAlpha;
		if (bgAlpha == 1) {
			context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
		}else{
			context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}
		context.getWindow().setAttributes(lp);
	}

	/**
	 * 监听弹层消失时的一些逻辑
	 * @param l
     */
	public void setOnDismissedListener(DialogDismissedListener l) {
		mDimissedListener = l;
	}

	/**
	 * 返回当前pop是否显示状态
	 * 
	 * @author reeta
	 */
	public boolean hasShowing() {
		 return  null == popWindow ? false : popWindow.isShowing();
	}

	/**
	 * 隐藏pop界面
	 * 
	 * @author reeta
	 */
	public void hidden() {
		
		mInstance = null;

		if (null == popWindow) {
			return;
		}
		backgroundAlpha(activity,1f);
		popWindow.dismiss();
	}

	/**
	 * 显示pop界面
	 * 
	 * @author reeta
	 */
	public void show() {
		if (hasShowing() || modeListAdapter.getItemCount() < 1) {
			return;
		}
		if(!activity.isFinishing()){
			if(null == view){
				view = activity.getWindow().getDecorView();
			}
			popWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
		}
	}
}
